package com.wordsay.web.controller;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.wordsay.web.analyst.WordsayAnalyst;
import com.wordsay.web.context.WebContext;

public class IndexController extends Controller {

	/**
	 * redirect into the wordsay index page
	 */
	public void index(){

		//this.renderHtml("index.html");
		this.render("index.vm");
	} 
	
	/**
	 *  redirect into the wordsay analyst result page
	 */
	public void entranceWordsay(){
		
		if(WebContext.isAnalysted){
			
			this.render("wordsay.vm");
			
		}else{ //redirect to the waiting web page, and execute the wordsay analyst
			
			
			ScheduledExecutorService execService =   Executors.newScheduledThreadPool(1);  
			execService.schedule(new Runnable(){
				
				@Override
				public void run(){
					
					try{
						
					
						(new WordsayAnalyst()).execute(); //execute the wordsay analyst
						WebContext.isAnalysted = true;

					}catch(Exception e){
						
						WebContext.isAnalysted = false;
					}
				}
			}, 2000, TimeUnit.MILLISECONDS);
			
			this.render("waiting.vm");
		}
		
	}
}